﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class RMVender : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindLinkman();
                BindGridView();
            }
        }

        #region Private Method

        private void BindLinkman()
        {
            LinkmanService linkmanServiceObj = ServiceFactory.getLinkmanService();

            OperationResult<List<tb_Linkman>> result = linkmanServiceObj.GetAllLinkman();

            if (result.operationSuccess)
            {
                ddlLinkman.DataSource = result.obj;
                ddlLinkman.DataTextField = "Name";
                ddlLinkman.DataValueField = "Id";
                ddlLinkman.DataBind();
            }
        }

        private void BindGridView()
        {
            RMVenderService rMVenderService = ServiceFactory.getRMVenderService();
            OperationResult<List<tb_RMVender>> result = rMVenderService.GetAllRMVender();

            if (result.operationSuccess)
            {
                gvRMVenderList.DataSource = result.obj;
                gvRMVenderList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            RMVenderService rMVenderServiceObj = ServiceFactory.getRMVenderService();
            LinkmanService linkmanServiceObj = ServiceFactory.getLinkmanService();

            OperationResult<List<tb_Linkman>> result = linkmanServiceObj.GetAllLinkman();

            if (result.operationSuccess)
            {
                ddlEditLinkman.DataSource = result.obj;
                ddlEditLinkman.DataTextField = "Name";
                ddlEditLinkman.DataValueField = "Id";
                ddlEditLinkman.DataBind();
            }
            else
            {
                //EXCEPTION 
                //return null;
            }

            OperationResult<tb_RMVender> resultEdit = rMVenderServiceObj.GetRMVenderById(id);
            if (result.operationSuccess == true)
            {
                txtEditName.Text = resultEdit.obj.Name;
                txtEditAddress.Text = resultEdit.obj.Address;
                txtEditBankInform.Text = resultEdit.obj.BankInformation;
                txtEditDescription.Text = resultEdit.obj.Description;
                txtEditFax.Text = resultEdit.obj.Fax;
                txtEditTel1.Text = resultEdit.obj.Tel1;
                txtEditTel2.Text = resultEdit.obj.Tel2;
                txtEditURL.Text = resultEdit.obj.URL;

                ddlEditLinkman.SelectedValue = resultEdit.obj.LinkMan.ToString();

                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();

            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        public string GetLinkmanNameById(string linkmanId)
        {
            string returnValue = "";
            int intLinkmanId = Int32.Parse(linkmanId);
            OperationResult<tb_Linkman> resultLinkmanEntity = ServiceFactory.getLinkmanService().GetLinkmanById(intLinkmanId);
            if (resultLinkmanEntity.operationSuccess)
            {
                returnValue = resultLinkmanEntity.obj.Name;
            }
                return returnValue;
        }

        #endregion

        #region Event
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            RMVenderService rMVenderServiceObj = ServiceFactory.getRMVenderService();
            LinkmanService linkmanServiceObj = ServiceFactory.getLinkmanService();

            tb_RMVender instance = new tb_RMVender();
            tb_Linkman linkmanInstance = new tb_Linkman();

            instance.Name = txtName.Text;
            instance.Address = txtAddress.Text;
            instance.BankInformation = txtBankInform.Text;
            instance.Description = txtDescription.Text;
            instance.Fax = txtFax.Text;
            instance.Tel1 = txtTel1.Text;
            instance.Tel2 = txtTel2.Text;
            instance.URL = txtURL.Text;

            instance.LinkMan = Int32.Parse(ddlLinkman.SelectedValue);

            OperationResult<tb_RMVender> result = rMVenderServiceObj.CreateRMVender(instance);
            BindGridView();

        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }

            RMVenderService rMVenderServiceObj = ServiceFactory.getRMVenderService();
            LinkmanService linkmanServiceObj = ServiceFactory.getLinkmanService();

            tb_RMVender entity = new tb_RMVender();
            tb_Linkman linkmanInstance = new tb_Linkman();


            entity.Id = editId;
            entity.Name = txtEditName.Text;
            entity.Address = txtEditAddress.Text;
            entity.BankInformation = txtEditBankInform.Text;
            entity.Description = txtEditDescription.Text;
            entity.Fax = txtEditFax.Text;
            entity.Tel1 = txtEditTel1.Text;
            entity.Tel2 = txtEditTel2.Text;
            entity.URL = txtEditURL.Text;

            entity.LinkMan = Int32.Parse(ddlEditLinkman.SelectedValue);
            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            linkmanInstance = linkmanServiceObj.GetLinkmanById(Int32.Parse(ddlEditLinkman.SelectedValue)).obj;
            entity.tb_Linkman = linkmanInstance;
            OperationResult<tb_RMVender> resultEdit = rMVenderServiceObj.ModifyRMVender(entity);

            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            RMVenderService rMVenderServiceObj = ServiceFactory.getRMVenderService();

            tb_RMVender deleteEntity = new tb_RMVender();
            deleteEntity.Id = deleteId;
            rMVenderServiceObj.DeleteRMVender(deleteEntity);

            BindGridView();
        }

        protected void gvRMVenderList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvRMVenderList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvRMVenderList.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        #endregion
    }
}
